#!/usr/bin/python

import unittest, sys, os, os.path as path, struct

if __name__ == "__main__":
    """Hack around the python import system to
    allow us to import from a parent directory"""
    FPATH = path.normpath(path.join(os.getcwd(), __file__))
    IPATH = path.normpath(path.join(path.dirname(FPATH),"../../"))
    sys.path.append(IPATH)

from gehyra.common.ipv4 import IPv4Address as I4


class TestIPv4AddressConstructor(unittest.TestCase):

    """Testing longs as well as ints because 2**32 on 32bit machines
    is a long"""

    def test_integer_min(self):
        i = I4(0)
        self.assertEqual(i.ip, 0)
        l = I4(0L)
        self.assertEqual(l.ip, 0)

    def test_integer_max(self):
        i = I4(2**32-1)
        self.assertEqual(i.ip, 2**32-1)
        l = I4(2L**32-1)
        self.assertEqual(l.ip, 2L**32-1)

    def test_negative_integer(self):
        self.assertRaises(ValueError, I4, -1)
        self.assertRaises(ValueError, I4, -1L)

    def test_above_max_integer(self):
        self.assertRaises(ValueError, I4, 2**32)
        self.assertRaises(ValueError, I4, 2L**32)

    def test_valid_hexstr_with_prefix(self):
        i = I4("0xFEDCBA98")
        self.assertEqual(i.ip, 0xFEDCBA98)

    def test_valid_hexstr_without_prefix(self):
        i = I4("FEDCBA98")
        self.assertEqual(i.ip, 0xFEDCBA98)

    def test_max_hexstr(self):
        i = I4("0xFFFFFFFF")
        self.assertEqual(i.ip, 0xFFFFFFFF)

    def test_min_hexstr(self):
        i = I4("0x00000000")
        self.assertEqual(i.ip, 0x00000000)

    def test_short_hexstr(self):
        self.assertRaises(ValueError, I4, "00")

    def test_long_hexstr(self):
        self.assertRaises(ValueError, I4, "0x123456789")

    def test_dot_dec_str(self):
        i = I4("192.168.0.1")
        self.assertEqual(i.ip, 0xC0A80001)

    def test_min_dot_dec_str(self):
        i = I4("0.0.0.0")
        self.assertEqual(i.ip, 0)
        self.assertEqual(i.ip, 0L)

    def test_max_dot_dec_str(self):
        i = I4("255.255.255.255")
        self.assertEqual(i.ip, 0xFFFFFFFF)
        self.assertEqual(i.ip, 0xFFFFFFFFL)

    def test_too_few_octets(self):
        self.assertRaises(ValueError, I4, "0.0.0")

    def test_octet_out_of_range(self):
        self.assertRaises(ValueError, I4, "192.168.256.1")

    def test_too_many_octets(self):
        self.assertRaises(ValueError, I4, "0.0.0.0.0")

    def test_min_packed(self):
        i = I4(struct.pack("!I", 0))
        self.assertEqual(i.ip, 0)
        self.assertEqual(i.ip, 0L)

    def test_max_packed(self):
        i = I4(struct.pack("!I", 0xFFFFFFFF))
        self.assertEqual(i.ip, 0xFFFFFFFF)
        self.assertEqual(i.ip, 0xFFFFFFFFL)

    def test_packed_octets(self):
        i = I4(struct.pack("!BBBB", 192, 168, 0, 1))
        self.assertEqual(i.ip, 0xC0A80001)
        self.assertEqual(i.ip, 0xC0A80001L)
        

class TestIPv4Getters(unittest.TestCase):
    
    def test_get_hex_without_prefix_implicit(self):
        i = I4("0xabcdef90")
        self.assertEqual("abcdef90", i.get_hex_string())

    def test_get_hex_without_prefix_explicit(self):
        i = I4("0x12345678")
        self.assertEqual("12345678", i.get_hex_string(False))

    def test_get_hex_with_prefix(self):
        i = I4("02468ACE")
        self.assertEqual("0x02468ace", i.get_hex_string(True))

    def test_get_hex_uppercase(self):
        i = I4("02468ace")
        self.assertEqual("02468ACE", i.get_hex_string(upper_case=True))
        
    def test_get_common_string_unexpanded_implicit(self):
        i = I4("000.001.020.200")
        self.assertEqual("0.1.20.200", i.get_common_string())

    def test_get_common_string_unexpanded_explicit(self):
        i = I4("2.3.4.5")
        self.assertEqual("2.3.4.5", i.get_common_string(False))

    def test_get_common_string_expanded(self):
        i = I4("0.0.0.0")
        self.assertEqual("000.000.000.000", i.get_common_string(True))

if __name__ == "__main__":
    unittest.main()

